<template>
  <scroll-page :loading="loading" :offset="offset" :no-data="noData" v-on:load="load">
    <article-item v-for="a in articles" :key="a.id" v-bind="a"></article-item>
  </scroll-page>
</template>

<script>
  import ArticleItem from '@/components/article/ArticleItem'
  import ScrollPage from '@/components/scrollpage'
  import {getArticles, getLikeBlogs} from '@/api/article'
  import {getUserId} from '@/request/token'   
  export default {
    name: "ArticleScrollPage",
    props: {
      offset: {
        type: Number,
        default: 100
      },
      page: {
        type: Object,
        default() {
          return {}
        }
      },
      query: {
        type: Object,
        default() {
          return {}
        }
      },
      userId: {
        type: String,
        default: '0'
      }
    },
    watch: {
      'query': {
        handler() {
          this.noData = false
          this.articles = []
          this.innerPage.pageNo = 1
          this.getArticles()
        },
        deep: true
      },
      'page': {
        handler() {
          this.noData = false
          this.articles = []
          this.innerPage = this.page
          this.getArticles()
        },
        deep: true
      }
    },
    data() {
      return {
        loading: false,
        noData: false,
        innerPage: {
          pageSize: 5,
          pageNo: 1,
          name: 'create_time',
          sort: 'desc'
        },
        articles: []
      }
    },
    components: {
      'article-item': ArticleItem,
        'scroll-page': ScrollPage
    },
    methods: {
      load() {
        this.getArticles()
      },
      view(id) {
        this.$router.push({path: `/view/${id}`})
      },
      getArticles() {
        let that = this
        that.loading = true
        let data = '';
        if(that.$route.path == '/') {
            data = {
                currentPage: that.innerPage.pageNo,
                own: 0,
                pageSize: 8
            }
        }else if(that.$route.path == '/1') {
            data = {
                currentPage: that.innerPage.pageNo,
                userId: that.$store.state.userInfo.id,
                pageSize: 8
            }
        }else if(that.$route.path == '/2') {
            data = {
                currentPage: that.innerPage.pageNo,
                userId: '1',
                pageSize: 8
            }
        }
        else if(that.$route.path == '/3') {
            data = {
                currentPage: that.innerPage.pageNo,
                userId: that.$store.state.userInfo.id,
                pageSize: 8
            }
            getLikeBlogs(data).then(data => {

            let newArticles = data.data
            if (newArticles && newArticles.length > 0) {
                that.innerPage.pageNo += 1
                that.articles = that.articles.concat(newArticles)
            } else {
                that.noData = true
            }
            return

            }).catch(error => {
            if (error !== 'error') {
                that.$message({type: 'error', message: '文章加载失败!', showClose: true})
            }
            }).finally(() => {
            that.loading = false
            })
        }
        getArticles(data).then(data => {

          let newArticles = data.data
          if (newArticles && newArticles.length > 0) {
            that.innerPage.pageNo += 1
            that.articles = that.articles.concat(newArticles)
          } else {
            that.noData = true
          }

        }).catch(error => {
          if (error !== 'error') {
            that.$message({type: 'error', message: '文章加载失败!', showClose: true})
          }
        }).finally(() => {
          that.loading = false
        })

      }
    },
    created() {
      this.getArticles();
      console.log(this.ownType);
    },
    watch: {
        "$route.path": function(newVal, oldVal) {
            if(newVal == '/2' || newVal == '/3' || newVal == '/') {
                this.getArticles();
            }
        }
    }
  }
</script>

<style scoped>
  .el-card {
    border-radius: 0;
  }

  .el-card:not(:first-child) {
    margin-top: 10px;

  }
</style>
